package com.atguigu.tingshu.album.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.CacheOperationType;
import com.atguigu.tingshu.common.cache.GuiGuCache;
import com.atguigu.tingshu.common.cache.GuiGuCached;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

    @Autowired
    private BaseCategory1Mapper baseCategory1Mapper;

    @Autowired
    private BaseCategory2Mapper baseCategory2Mapper;

    @Autowired
    private BaseCategory3Mapper baseCategory3Mapper;

    @Autowired
    private BaseCategoryViewMapper baseCategoryViewMapper;

    @Autowired
    private BaseAttributeMapper baseAttributeMapper;

    /**
     * 获取分类(1-2-3) 此时用视图解决连表查询
     * @return
     */
    @Override
    public List<JSONObject> getBaseCategoryList() {
        // 获取所有分类
        List<BaseCategoryView> baseCategoryViews = baseCategoryViewMapper.selectList(null);
        // 创建所有 List<JSONObject> 集合
        ArrayList<JSONObject> list1 = new ArrayList<>();
        // 去重分组
        // 1、一级标题去重分组
        Map<Long, List<BaseCategoryView>> listMap1 = baseCategoryViews.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
        for (Map.Entry<Long, List<BaseCategoryView>> longListEntry1 : listMap1.entrySet()) {
            // 获取一级标题 id、name
            Long categoryId_one = longListEntry1.getKey();
            String categoryName_one = longListEntry1.getValue().get(0).getCategory1Name();
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("categoryId", categoryId_one);
            jsonObject1.put("categoryName", categoryName_one);
            // 获取二级标题分类
            Map<Long, List<BaseCategoryView>> listMap2 = longListEntry1.getValue().stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            ArrayList<JSONObject> list2 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntry2 : listMap2.entrySet()) {
                // 获取二级标题 id、name
                Long categoryId_two = longListEntry2.getKey();
                String categoryName_two = longListEntry2.getValue().get(0).getCategory2Name();
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", categoryId_two);
                jsonObject2.put("categoryName", categoryName_two);

                ArrayList<JSONObject> list3 = new ArrayList<>();
                for (BaseCategoryView baseCategoryView : longListEntry2.getValue()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                    jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                    list3.add(jsonObject3);
                    // 构建树形结构--2、三级标题分类
                    jsonObject2.put("categoryChild", list3);
                }
                list2.add(jsonObject2);
            }
            // 构建树形结构--1、二级标题分类
            jsonObject1.put("categoryChild", list2);
            // 构建树形结构--0、一级标题分类
            list1.add(jsonObject1);
        }

        return list1;
    }

    /**
     * 连表查询
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseAttribute> findAttribute(Long category1Id) {
        List<BaseAttribute> baseAttributes = baseAttributeMapper.findAttribute(category1Id);
        return baseAttributes;
    }

    @Override
    //@GuiGuCache(prefix = "category3View:")
    @GuiGuCached(prefix = "album:categoryview:")
    public BaseCategoryView getCategoryView(Long category3Id) {
        BaseCategoryView baseCategoryView = baseCategoryViewMapper.selectById(category3Id);
        return baseCategoryView;
    }

    /**
     * 根据 category1Id 查询 category3List 并且取置顶的前七个
     * @param category1Id
     * @return
     */
    @Override
    public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
        // 1.先根据一级分类Id 查询 二级分类列表
        LambdaQueryWrapper<BaseCategory2> baseCategory2Wrapper = new LambdaQueryWrapper<>();
        baseCategory2Wrapper.eq(BaseCategory2::getCategory1Id, category1Id);
        List<BaseCategory2> baseCategory2List = baseCategory2Mapper.selectList(baseCategory2Wrapper);
        if(CollectionUtil.isNotEmpty(baseCategory2List)){
            // 2.根据二级分类Id 获取 三级分类列表
            List<Long> category2 = baseCategory2List.stream().map(BaseCategory2::getId).collect(Collectors.toList());
            LambdaQueryWrapper<BaseCategory3> baseCategory3Wrapper = new LambdaQueryWrapper<>();
            //2.1 查询二级分类下包含三级分类
            baseCategory3Wrapper.in(BaseCategory3::getCategory2Id, category2);
            //2.2 查询需要置顶的三级分类
            baseCategory3Wrapper.eq(BaseCategory3::getIsTop, 1);
            //2.3 根据序号排序 升序排
            baseCategory3Wrapper.orderByAsc(BaseCategory3::getOrderNum);
            //2.4 截取前7个 SQL+limit 7
            baseCategory3Wrapper.last("limit 7");
            List<BaseCategory3> baseCategory3List = baseCategory3Mapper.selectList(baseCategory3Wrapper);
            return baseCategory3List;

        }

        return null;
    }

    @Override
    public JSONObject getBaseCategoryListById(Long category1Id) {
        // 1.获取一级分类列表
        LambdaQueryWrapper<BaseCategoryView> baseCategoryViewWrapper = new LambdaQueryWrapper<>();
        baseCategoryViewWrapper.eq(BaseCategoryView::getCategory1Id, category1Id);
        List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(baseCategoryViewWrapper);
        // 1.1 封装 category1
        if(CollectionUtil.isNotEmpty(baseCategoryViewList)){
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("categoryId", baseCategoryViewList.get(0).getCategory1Id());
            jsonObject1.put("categoryName", baseCategoryViewList.get(0).getCategory1Name());
            // 2.获取二级分类列表
            Map<Long, List<BaseCategoryView>> listMap = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            // 2.1 封装 category2
            ArrayList<JSONObject> list1 = new ArrayList<>();
            for (Map.Entry<Long, List<BaseCategoryView>> longListEntry : listMap.entrySet()) {
                JSONObject jsonObject2 = new JSONObject();
                jsonObject2.put("categoryId", longListEntry.getKey());
                jsonObject2.put("categoryName", longListEntry.getValue().get(0).getCategory2Name());
                list1.add(jsonObject2);
                ArrayList<JSONObject> list2 = new ArrayList<>();
                // 3.获取三级分类列表
                for (BaseCategoryView baseCategoryView : longListEntry.getValue()) {
                    JSONObject jsonObject3 = new JSONObject();
                    jsonObject3.put("categoryId", baseCategoryView.getCategory3Id());
                    jsonObject3.put("categoryName", baseCategoryView.getCategory3Name());
                    list2.add(jsonObject3);
                    list2.add(jsonObject3);
                }
                // 3.封装 category3
                jsonObject2.put("categoryChild", list2);
            }
            jsonObject1.put("categoryChild", list1);
            return jsonObject1;
        }
        return null;
    }
}
